package com.repast.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.repast.common.Const;
import com.repast.common.Const.AddressType;
import com.repast.common.ServerResponse;
import com.repast.dao.AddressMapper;
import com.repast.pojo.Address;
import com.repast.service.IAddressService;
import com.repast.utils.DateUtils;

@Service
public class AddressServiceImpl implements IAddressService {

	@Autowired
	private AddressMapper addressMapper;
	
	public ServerResponse selectAddressList(String openid) {
		List<Address> list = addressMapper.selectAddressList(openid);
		if (list.size() == 0) {
			return ServerResponse.createByErrorCodeMessage(Const.NO_HAVE_ADDRESS, "你当前还没有添加任何地址");
		}
		return ServerResponse.createBySuccess(list);
	}

	public ServerResponse changeStatus(String openid, Integer addressId) {
		// 为防止多个默认地址根据openid查询所有的地址,
		List<Address> list = addressMapper.selectAddressList(openid);
		for (Address address : list) {
			if (address.getAddressId() != addressId) {
				address.setAddressStatus(Const.AddressType.PUTONG_ADDRESS);
			} else {
				address.setAddressStatus(Const.AddressType.DEFAULT_ADDRESS);
			}
			address.setAddressUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
			addressMapper.updateByPrimaryKeySelective(address);
		}
		return ServerResponse.createBySuccess();
	}

	public ServerResponse insertAddress(Address address) {
		List<Address> list = addressMapper.selectDeliveryInfoByOpenid(address.getOpenid(), Const.AddressType.DEFAULT_ADDRESS);
		if(list.size()!=0){
			list.get(0).setAddressStatus(Const.AddressType.PUTONG_ADDRESS);
			address.setAddressUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
			addressMapper.updateByPrimaryKeySelective(list.get(0));
		}
		address.setAddressCreateTime(DateUtils.formatDateTime(new Date(), "long"));
		address.setAddressUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
		int insertAddress = addressMapper.insert(address);
		if (insertAddress == 0) {
			return ServerResponse.createByErrorCodeMessage(Const.INSERT_ADDRESS_FAIL, "地址插入失败");
		}
		return ServerResponse.createBySuccess(address.getAddressId());
	}

	public ServerResponse deleteAddress(Integer addressId) {
		int deleteAddress = addressMapper.deleteByPrimaryKey(addressId);
		if (deleteAddress == 0) {
			return ServerResponse.createByErrorCodeMessage(Const.INSERT_ADDRESS_FAIL, "删除地址失败");
		}
		return ServerResponse.createBySuccess();
	}

	public ServerResponse updateAddress(Address address) {
		List<Address> list = addressMapper.selectDeliveryInfoByOpenid(address.getOpenid(), Const.AddressType.DEFAULT_ADDRESS);
		if(list.size()!=0){
			list.get(0).setAddressStatus(Const.AddressType.PUTONG_ADDRESS);
			address.setAddressUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
			addressMapper.updateByPrimaryKeySelective(list.get(0));
		}
		address.setAddressUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
		int updateAddress = addressMapper.updateByPrimaryKeySelective(address);
		if (updateAddress == 0) {
			return ServerResponse.createByErrorCodeMessage(Const.UPDATE_ADDRESS_FAIL, "更新地址失败");
		}
		return ServerResponse.createBySuccess();
	}

	public ServerResponse selectDeliveryInfoByOpenid(String openid) {
		List<Address> list = addressMapper.selectDeliveryInfoByOpenid(openid, Const.AddressType.DEFAULT_ADDRESS);
		if (list.size() == 0) {
			return ServerResponse.createByErrorCodeMessage(Const.NO_DEFAULT_ADDRESS, "你还没有添加默认的收货地址");
		}
		if (list.size() > 1) {
			return ServerResponse.createByErrorCodeMessage(Const.MANY_DEFAULT_ADDRESS, "系统数据错误,你有多个默认收货地址,请联系管理员!");
		}
		return ServerResponse.createBySuccess(list.get(0));
	}

}
